do "...\First.do"

********************************************************************************
* Hospital admissions
********************************************************************************

clear
use "$raw\patient_hospital_moreyears.dta"
drop if missing(pnr)

merge 1:m recnum using "$raw\diagnosis_moreyears.dta"
drop if _merge==2

drop year _merge
g year=year(d_inddto)

sort pnr year
order pnr year

drop if year<1995

g letter=substr(c_diag,2,1) // Letter in ICD-10 code
g number2=real(substr(c_diag,3,2)) // Number in ICD-10 code
g number3=real(substr(c_diag,3,3))


g ACSC_COPD=(letter=="J" & inlist(number2,40,41,21,43,47,20))
replace ACSC_COPD=1 if (letter=="J" & inlist(number3,440,441,449))

keep if ACSC_COPD==1 

collapse (max) ACSC_COPD, by(pnr year)

save "$work\hospital_admissions.dta", replace







